package com.example.demo.error;

import org.apache.shiro.ShiroException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.example.demo.common.BaseResult;

@RestControllerAdvice
public class ErrorController {

	/**
	 * 捕捉 CustomRealm 抛出的异常
	 */
	@ExceptionHandler(ShiroException.class)
	public BaseResult handleShiroException() {
		return new BaseResult(401, "您没有权限访问 : ", null);
	}

	/**
	 * 请求没有带有 token 或者带了 token 但权限认证不通过，则会报 UnauthenticatedException 异常
	 * 
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public BaseResult handle401(Exception ex) {
		return new BaseResult(500, ex.getMessage(), null);
	}
}
